void moverCursor (unsigned char lin, unsigned char col) {
	unsigned short offset;
	if ( lin < 1 ) lin = 1;
	if ( col > 25 ) col = 25;
	lin--;
	col--;
	offset = (lin<<6) + (lin<<4) + col; //lin*80 + col
	
	outb(0x3D4, 14);
	outb(0x3D5, offset>>8);
	outb(0x3D4, 15);
	outb(0x3D5, offset);
}

void imprimir(const char *cadena) {
	unsigned short offset;
	unsigned long i;
	unsigned char *vidmem = (unsigned char *)0xB8000;


	// Read cursor position
	outb(0x3D4, 14);
	offset = inb(0x3D5) << 8;
	outb(0x3D4, 15);
	offset |= inb(0x3D5);

	// Start at writing at cursor position
	vidmem += offset*2;

	// Continue until we reach null character
	i = 0;
	while (cadena[i] != 0) {
		*vidmem = cadena[i++];
		vidmem += 2;
	}

	// Set new cursor position
	offset += i;
	outb(0x3D5, (unsigned char)(offset));
	outb(0x3D4, 14);
	outb(0x3D5, (unsigned char)(offset >> 8));
}

void imprimirHex(unsigned int num) {
	char c[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	char *t = "0x00000000\0";

	t[2] = c[(num>>28) & 0x0F];
	t[3] = c[(num>>24) & 0x0F];
	t[4] = c[(num>>20) & 0x0F];
	t[5] = c[(num>>16) & 0x0F];
	t[6] = c[(num>>12) & 0x0F];
	t[7] = c[(num>>8) & 0x0F];
	t[8] = c[(num>>4) & 0x0F];
	t[9] = c[num & 0x0F];
	imprimir(t);
}

void limpiarPant() {
	unsigned char *vidmem = (unsigned char *)0xB8000;
	const long size = 2000; //80*25
	long loop;

	for (loop=0; loop<size; loop++) {
		*vidmem++ = 0;
		*vidmem++ = 0x7;
	}

	moverCursor(1,1);
}
